package com.shj.em.biz.prod.controller;

import com.shj.em.biz.sell.entity.Order;
import com.shj.em.biz.hr.service.EmpService;
import org.springframework.stereotype.Controller;

import java.util.Map;
import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.shj.em.common.annotation.Log;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;

import com.shj.em.biz.prod.entity.OrderCnc;
import com.shj.em.biz.prod.service.OrderCncService;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import com.shj.em.base.controller.BaseController;


/**
 * <p>
 * 订单程序段表 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-11-19
 */
@Controller
@Slf4j
@RequestMapping("/orderCnc")
public class OrderCncController extends BaseController<OrderCnc>{

    private String prefix = "prod/ordercnc";

    @Autowired
    OrderCncService ordercncService;

    @Autowired
    EmpService empService;

    @GetMapping("/gotoSearch")
    public String gotoSearch(){
        return prefix + "/search";
    }

    /*@RequestMapping("/orderList")
    @ResponseBody
    public PageVO<Order> orderList(@RequestBody Map<String, Object> params){
        Page<Order> page = orderService.queryOrderPage(params);
        PageVO<Order> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(page.getRecords());
        return pageVO;
    }*/

    @GetMapping("/gotoGenProgram/{orderId}")
    public String gotoGenProgram(@PathVariable("orderId") Long orderId, Model model){
        model.addAttribute("order", orderService.selectById(orderId));
        return prefix + "/orderCnc";
    }

    @GetMapping("/gotoViewProgram/{orderId}")
    public String gotoViewProgram(@PathVariable("orderId") Long orderId, Model model){
        model.addAttribute("order", orderService.selectById(orderId));
        return prefix + "/viewCnc";
    }

    @PostMapping("/loadJobs")
    @ResponseBody
    public PageVO<OrderCnc> loadJobs(@RequestBody Map<String, Object> params){
        Page<OrderCnc> list = ordercncService.queryOrderCncPage(params);
        PageVO<OrderCnc> pageVO = new PageVO<>();
        pageVO.setTotal(list.getTotal());
        pageVO.setRows(list.getRecords());
        return pageVO;
    }

    @GetMapping("/gotoAdd/{orderId}")
    String gotoAdd(@PathVariable("orderId")Long orderId, Model model){
        OrderCnc cnc = new OrderCnc();
        cnc.setOrderId(orderId);
        model.addAttribute("isEdit", false);
        model.addAttribute("ordercnc", cnc);
        model.addAttribute("programEmps", empService.findAllProgramEmps());
        return prefix + "/add";
    }

    @GetMapping("/gotoEdit/{id}")
    String gotoEdit(@PathVariable("id") Long id, Model model){
        OrderCnc ordercnc = ordercncService.selectById(id);
        model.addAttribute("ordercnc", ordercnc);
        model.addAttribute("isEdit", true);
        model.addAttribute("programEmps", empService.findAllProgramEmps());
        return prefix + "/add";
    }

    /*@Log("创建订单程序段表")
    @ResponseBody
    @PostMapping("/add")
    public ResMsg add(OrderCnc ordercnc){
        ordercncService.insert(ordercnc);
        return ResMsg.ok();
    }*/

    @PostMapping("/add")
    public String add(OrderCnc ordercnc, Model model){
        ordercncService.insertCnc(ordercnc);
        model.addAttribute("ordercnc", ordercnc);
        model.addAttribute("isEdit", true);
        model.addAttribute("programEmps", empService.findAllProgramEmps());
        return prefix + "/add";
    }

    @Log("更新订单程序段表")
	@ResponseBody
	@PostMapping("/edit")
	public ResMsg edit(OrderCnc ordercnc){
        ordercncService.updateById(ordercnc);
        return ResMsg.ok();

	}

	@Log("删除订单程序段表")
	@PostMapping( "/remove")
	@ResponseBody
	public ResMsg remove(@RequestParam("id") Long id){
        try{
            ordercncService.deleteById(id);
            return ResMsg.ok();
        }catch(Exception e){
            return ResMsg.error();
        }
	}

	@Log("批量删除订单程序段表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	public ResMsg batchRemove(@RequestParam("ids[]") Long[] ids){
        ordercncService.deleteBatchIds(Arrays.asList(ids));
		return ResMsg.ok();
	}

}

